package com.guigu.qingcheng.orderservice.controller;


import com.guigu.qingcheng.orderservice.mapper.TbOrderItemMapper;
import com.guigu.qingcheng.orderservice.service.TbOrderItemService;
import com.guigu.qingcheng.pojo.order.TbOrderItem;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("orderitem")
public class TbOrderItemController {
    @Autowired
    private TbOrderItemService tbOrderItemService;
    @Resource
    private TbOrderItemMapper mapper;

    @GetMapping("findById")
    public R findById (Integer id) {
        TbOrderItem tbOrderItem = tbOrderItemService.getById(id);
        if (tbOrderItem  == null)
            return R.error();
        return R.ok().data("tbOrderItem",tbOrderItem);
    }

    //查询分页查询
    @PostMapping("findPage")
    public R findPage (@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody(required = false) Map<String,Object> searchMap ) {
        return tbOrderItemService.findPage(page, size, searchMap);
    }

    //新增
    @PostMapping("add")
    public R add (@RequestBody TbOrderItem tbOrderItem) {
        boolean save = tbOrderItemService.save(tbOrderItem);
        if (save)
            return R.ok();
        return R.error();
    }

    //修改
    @PostMapping("update")
    public R update (@RequestBody TbOrderItem tbOrderItem) {
        boolean save = tbOrderItemService.updateById(tbOrderItem);
        if (save)
            return R.ok();
        return R.error();
    }

    //修改明细表
    @PostMapping("updates")
    public R updates (@RequestBody List<TbOrderItem> tbOrderItems,@RequestParam("id") String id) {

        return tbOrderItemService.updates(tbOrderItems,id);
    }

    //删除
    @GetMapping("delete")
    public R delete (String id) {
        boolean save = tbOrderItemService.removeById(id);
        if (save)
            return R.ok();
        return R.error();
    }

    //根据订单编号来查询
    @GetMapping("findByOrderId")
    public R findByOrderId (String id) {
        List<TbOrderItem> byOrderId = tbOrderItemService.findByOrderId(id);
        return R.ok().data("list",byOrderId);
    }

    @GetMapping("goodById")
    public R goodById (String id) {
        List<Map> list = mapper.listgoods(id);
        if (list.size()<0)
            return R.error();
        return R.ok().data("goods",list);
    }


    //根据订单编号来查询
    @GetMapping("findByOrderIdlj")
    public R findByOrderIdlj (String id) {
        List<Map> byOrderIdlj = tbOrderItemService.findByOrderIdlj(id);
        return  R.ok().data("list",byOrderIdlj);
    }
}

